package com.bw.services.viewmodel

import androidx.lifecycle.Lifecycle
import androidx.lifecycle.repeatOnLifecycle
import androidx.lifecycle.viewModelScope
import com.bw.common.utils.LogUtil
import com.bw.lib_base.base.BaseViewModel
import com.bw.services.entity.AddParkingEntity
import com.bw.services.entity.CarManage
import com.bw.services.entity.CarParkingEntity
import com.bw.services.entity.NewsEntity
import com.bw.services.model.ServicesRepo
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import javax.inject.Inject
import kotlin.math.log

@HiltViewModel
class ServicesViewModel @Inject constructor(val repo: ServicesRepo):BaseViewModel() {
    private val _servicesData = MutableStateFlow<CarManage?>(null)
    val serviceData: MutableStateFlow<CarManage?> =_servicesData
    fun getServices(licensePlate: String){
        viewModelScope.launch {
            LogUtil.d("===","getServices:执行了")
            if(isInitLife()){
                LogUtil.d("===","生命周期:执行了")
                lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED){
                    val result = repo.getCar_Manage(licensePlate)
                    _servicesData.emit(result)
                }
            }
        }
    }
    private val _carParkingData = MutableStateFlow<List<CarParkingEntity>?>(null)
    val carParking :StateFlow<List<CarParkingEntity>?> =_carParkingData
    fun getCarParking(areaId: Int){
        viewModelScope.launch {
            LogUtil.d("===","getCarParking:执行了")
            if(isInitLife()){
                LogUtil.d("===","生命周期:执行了")
                lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED){
                    val result = repo.getCar_Parking(areaId)
                    LogUtil.d("===",result.toString())
                    LogUtil.d("===","======")
                    _carParkingData.emit(result)
                }
            }
        }
    }
    private val _addParkingData = MutableStateFlow<CarParkingEntity?>(null)
    val addParking :StateFlow<CarParkingEntity?> =_addParkingData.asStateFlow()
    suspend fun getCar_Add_Parking(addParking: CarParkingEntity){
       try {
            LogUtil.d("===","getCar_Add_Parking:执行了")
            val result =repo.getCar_Add_Parking(addParking)
            _addParkingData.value =result
        }catch (e:Exception){
            _addParkingData.value =null
            LogUtil.d("===",e.message.toString())
        }
    }

    private val _upDataParkingData = MutableStateFlow<CarParkingEntity?>(null)
    val upDataParking :StateFlow<CarParkingEntity?> =_upDataParkingData.asStateFlow()

    suspend fun getUpData_Parking(id: Int,upData: CarParkingEntity){
        try {
            LogUtil.d("===","getUpData_Parking:执行了")
            val result =repo.getUpData_Parking(id,upData)
            _upDataParkingData.value =result
        }catch (e:Exception){
            _upDataParkingData.value =null
        }
    }

    private val _newsData = MutableStateFlow<List<NewsEntity>?>(null)
    val newsData :StateFlow<List<NewsEntity>?> =_newsData.asStateFlow()
    fun getNews(){
        viewModelScope.launch {
            LogUtil.d("===","getNews:执行了")
            if(isInitLife()){
                LogUtil.d("===","生命周期:执行了")
                lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED){
                    val result = repo.getNews()
                    _newsData.emit(result)
                }
            }
        }
    }
    private val _addNewsData = MutableStateFlow<NewsEntity?>(null)
    val addNewsData :StateFlow<NewsEntity?> =_addNewsData.asStateFlow()
    suspend fun get_addNews(body: NewsEntity){
        try {
            LogUtil.d("===","get_addNews:执行了")
            val result =repo.get_addNews(body)
            _addNewsData.value =result
        }catch (e:Exception){
            _addNewsData.value =null
        }
    }
}